<?php
/**
 * Created by PhpStorm.
 * User: axie
 * Date: 03/05/17
 * Time: 5:19 PM
 */
namespace HQ\MissingAddressRegionFix\Observer;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Directory\Model\RegionFactory;
class AddressFormat implements ObserverInterface
{
    protected $_regionFactory;
    public function __construct(     
        \Magento\Directory\Model\RegionFactory $regionFactory
    )
    {
        $this->_regionFactory = $regionFactory;
    }
    /**
     * Save region if address object has region_id but not region name
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return $this
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $address = $observer->getEvent()->getAddress();
        if($address->getRegion() == null)
        {
            $regionId = $address->getRegionId();
            /** @var \Magento\Directory\Model\Region $region */
            $region = $this->_regionFactory->create();
            $region->getResource()->load($region,$regionId);
            $address->setRegion($region->getName());
            $address->setRegionCode($region->getCode());
            $address->save();
        }
    }
}